local gongxin = fk.CreateSkill {

  name = "joy__gongxin",

  tags = {  },

}



gongxin:addEffect(fk.TargetSpecified, {
  name = "joy__gongxin",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if player == target and player:hasSkill(gongxin.name)
    and player:usedSkillTimes(gongxin.name, Player.HistoryTurn) == 0 and #data:getAllTargets() == 1 then
      local to = (event == fk.TargetSpecified) and data.to or data.from
      if to and to ~= player.id and not player.room:getPlayerById(to):isKongcheng() then
        event:setCostData(self, to)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local cards = to.player_cards[Player.Hand]
    local red = table.filter(cards, function (id) return Fk:getCardById(id).color == Card.Red end)
    local ids, choice = U.askforChooseCardsAndChoice(player, red, {"joy__gongxin_obtaincard", "gongxin_put"},
    gongxin.name, "#joy__gongxin-view", {"Cancel"}, 1, 1, cards)
    if #ids > 0 then
      player:showCards(ids)
      if choice == "joy__gongxin_obtaincard" then
        room:moveCardTo(ids,Card.PlayerHand,player,fk.ReasonPrey,gongxin.name,nil,true,player)
      else
        room:moveCardTo(ids, Card.DrawPile, nil, fk.ReasonPut, gongxin.name, nil, true)
      end
    end
  end,
})
gongxin:addEffect(fk.TargetConfirmed, {
  name = "joy__gongxin",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if player == target and player:hasSkill(gongxin.name)
    and player:usedSkillTimes(gongxin.name, Player.HistoryTurn) == 0 and #data:getAllTargets() == 1 then
      local to = (event == fk.TargetSpecified) and data.to or data.from
      if to and to ~= player.id and not player.room:getPlayerById(to):isKongcheng() then
        event:setCostData(self, to)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local cards = to.player_cards[Player.Hand]
    local red = table.filter(cards, function (id) return Fk:getCardById(id).color == Card.Red end)
    local ids, choice = U.askforChooseCardsAndChoice(player, red, {"joy__gongxin_obtaincard", "gongxin_put"},
    gongxin.name, "#joy__gongxin-view", {"Cancel"}, 1, 1, cards)
    if #ids > 0 then
      player:showCards(ids)
      if choice == "joy__gongxin_obtaincard" then
        room:moveCardTo(ids,Card.PlayerHand,player,fk.ReasonPrey,gongxin.name,nil,true,player)
      else
        room:moveCardTo(ids, Card.DrawPile, nil, fk.ReasonPut, gongxin.name, nil, true)
      end
    end
  end,
})

return gongxin